<?php
	defined('_JEXEC') or die('Restricted access');
	//echo "path:".CUTEENGINE_UTILS_COMPONENTS_PATH;
	require_once(CUTEENGINE_BASE_PART_PATH."base_part_class.php");
    
	//
    // t_table_part
    //
	class t_table_part extends t_base_part
	{
        // properties declaration
		// public
        // D
        public $db_fields = array(); 
        // E
        public $events = array(
            1=>t_base_field::field_event_type_on_selected,
            2=>t_base_field::field_event_type_on_edit,
			3=>t_base_field::field_event_type_on_cancel,
            4=>t_base_field::field_event_type_on_save,
            5=>t_base_field::field_event_type_on_insert,
            6=>t_base_field::field_event_type_on_reload,
            7=>t_base_field::field_event_type_on_delete,
            8=>t_base_field::field_event_type_on_paging);

		const events_position_on_select = 1;
        const events_position_on_edit = 2;
        const events_position_on_cancel = 3;
        const events_position_on_save = 4;
		const events_position_on_insert = 5;
        const events_position_on_reload = 6;
        const events_position_on_delete = 7;
        const events_position_on_paging = 8;
        const events_position_last = 9;
        // F
        const fields_position_table = 1;
        const fields_position_last = 2;        
        // R
        const required_actions_position_source = 1;
        const required_actions_position_last = 2;
		// S
		const specific_parameters_position_per_SQL = 1;
		const specific_parameters_position_per_SQL_condition_values = 2;
		const specific_parameters_position_last = 3;
        public $sql_table = ''; 
        // properties declaration
		// protected
        
        // method declaration
		// protected
        // E
        // G
        protected function  generate_events_actions($a_events_array)
        {
            //echo "event events_position_on_select: ".$a_events_array[(self::events_position_on_select)]."<br>";
            if (strtoupper($a_events_array[(self::events_position_on_select)]) === 'TRUE') 
            {
				
				$this->add_event(self::events_position_on_select,
                    self::required_actions_position_source,
                    self::fields_position_table);
            } 
            if (strtoupper($a_events_array[(self::events_position_on_edit)]) === 'TRUE')
            {
				
				$this->add_event(self::events_position_on_edit,
                    self::required_actions_position_source,
                    self::fields_position_table);
            } 
            if (strtoupper($a_events_array[(self::events_position_on_cancel)]) === 'TRUE')
            {
				
				$this->add_event(self::events_position_on_cancel,
                    self::required_actions_position_source,
                    self::fields_position_table);
            } 			
            if (strtoupper($a_events_array[(self::events_position_on_save)]) === 'TRUE')
            {
				
				$this->add_event(self::events_position_on_save,
                    self::required_actions_position_source,
                    self::fields_position_table);
            }           
            if (strtoupper($a_events_array[(self::events_position_on_insert)]) === 'TRUE')
            {
				
				$this->add_event(self::events_position_on_insert,
                    self::required_actions_position_source,
                    self::fields_position_table);
            }  			
            if (strtoupper($a_events_array[(self::events_position_on_reload)]) === 'TRUE')
            {
				
				$this->add_event(self::events_position_on_reload,
                    self::required_actions_position_source,
                    self::fields_position_table);
            } 
            if (strtoupper($a_events_array[(self::events_position_on_delete)]) === 'TRUE')
            {
				
				$this->add_event(self::events_position_on_delete,
                    self::required_actions_position_source,
                    self::fields_position_table);
            }             
            if (strtoupper($a_events_array[(self::events_position_on_paging)]) === 'TRUE')
            {
				
				$this->add_event(self::events_position_on_paging,
                    self::required_actions_position_source,
                    self::fields_position_table);
            }
        }
        protected function get_child_action_of_parent_part()
        {
            return $this->required_actions[self::required_actions_position_source];
        }
        
		protected function get_events_array()
        {
            return $this->events;
        }
		protected function get_events_position_last()
        {
            return self::events_position_last;
        }
		public function get_field_by_step($a_step)
		{
			return null;
		}
        protected function get_fields_position_last()
		{
            return self::fields_position_last;
        }

        protected function get_parent_action_name()
		{
			$local_index = ((self::required_actions_position_last - 1) + (self::events_position_on_select));
           // echo "local_index: $local_index<br>";
			$local_return = $this->required_actions[$local_index];
			return $local_return->get_action_name();
        }
        protected function get_required_actions_position_last()
        {
            return self::required_actions_position_last;
        }
        static function get_help_specific_parameter($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::specific_parameters_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_specific_parameter($a_index)
		{
			$local_return = '';
			//echo "a_index: $a_index<br>";
			switch ($a_index) 
            {
                /*case self::specific_parameters_position_:
					$local_return = 'TRUE';
				break;                */
			}
			return $local_return;
		}		
        protected function get_specific_parameters_position_last()
        {
            return self::specific_parameters_position_last;
        }

		
        // method declaration
        // public
        public function __construct($a_parent_component,$a_part_id,$a_part_order,$a_part_type,
			$a_part_common_parameters,$a_part_specific_parameters,
			$a_specific_fields_string,$a_required_actions_string,$a_part_enabled) 
		{
            parent::__construct($a_parent_component,$a_part_id,$a_part_order,$a_part_type,
				$a_part_common_parameters,$a_part_specific_parameters,
				$a_specific_fields_string,$a_required_actions_string,$a_part_enabled);
                
            
            
		}
		//A
        public function add_event($a_field_event_type_index,$a_parent_required_action_index,$a_field_index)
        {
		            //echo __METHOD__;
					
			parent::add_event($a_field_event_type_index,$a_parent_required_action_index,$a_field_index);		
			$local_events = $this->get_events_array();
			$local_field_event_type = $local_events[$a_field_event_type_index];
            $local_field = $this->fields[$this->fields_positions[$a_field_index]];
            $local_action = $this->required_actions[$a_parent_required_action_index];
            $local_parent_component = $this->parent_component;
            if(method_exists($local_parent_component,'get_json_condition_values'))
				$local_json_condition_values = $local_parent_component->get_json_condition_values();
			else
				$local_json_condition_values = "[]";
            switch ($local_field_event_type) 
            {
				/*********** t_base_field::field_event_type_on_reload ***********/
                case t_base_field::field_event_type_on_reload:
                    $this->add_required_action('["'.$this->get_action_name_of_part_event($local_field_event_type,"t_action_set_event_part").'","t_action_set_values_from_db","'.$this->get_action_name_of_part_event($local_field_event_type,"t_action_set_values_from_db").'",['.
                        '"","","'.$local_field->name.'",""],"'.$this->specific_parameters[self::specific_parameters_position_per_SQL].'",'.$local_json_condition_values.',[""],[""]]',
                        (($this->get_required_actions_position_last()-1) + $a_field_event_type_index));
                   
                break;  
				/*********** t_base_field::field_event_type_on_paging ***********/
                case t_base_field::field_event_type_on_paging:
                    $this->add_required_action('["'.$this->get_action_name_of_part_event($local_field_event_type,"t_action_set_event_part").'","t_action_set_values_from_db","'.$this->get_action_name_of_part_event($local_field_event_type,"t_action_set_values_from_db").'",['.
                        '"","","'.$local_field->name.'",""],"'.$this->specific_parameters[self::specific_parameters_position_per_SQL].'",'.$local_json_condition_values.',[""],[""]]',
                        (($this->get_required_actions_position_last()-1) + $a_field_event_type_index));
                   
                break;                  

            }   
		}
		//G
        public function get_call_back_action($a_event_type)
        {
            $local_action = parent::get_call_back_action($a_event_type);
			switch ($a_event_type) 
            {
				/*********** t_base_field::field_event_type_on_reload ***********/
                case t_base_field::field_event_type_on_reload:
                    $local_action = "t_action_set_values_from_db";
                break;                
				/*********** t_base_field::field_event_type_on_paging ***********/
                case t_base_field::field_event_type_on_paging:
                    $local_action = "t_action_set_values_from_db";                
				break;		
            }  
            //echo "local_action: $local_action - a_event_type:$a_event_type<br>";
			return $local_action; 
        }        
		public function get_json_db_fields_list()
        {
            
			return str_replace('\/','/',json_encode($this->db_fields));
        }	
        // S
        public function set_db_fields($a_query_parameter,$a_current_field,
			$a_current_field_no,$a_fields_count,$a_current_row_no,$a_rows_count,$a_page_field)
        {
            $this->sql_table = $a_query_parameter->main_table_name;
            //echo "this->sql_table: $this->sql_table<br>";
            $this->db_fields[] = $a_current_field;
            

			
		}

        

    }
?>    